package com.syyo.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.gulimall.coupon.domain.entity.SeckillSessionEntity;
import com.syyo.gulimall.coupon.domain.entity.SeckillSkuRelationEntity;
import com.syyo.gulimall.coupon.domain.req.SeckillSessionReq;
import com.syyo.gulimall.coupon.mapper.SeckillSessionMapper;
import com.syyo.gulimall.coupon.mapper.SeckillSkuRelationMapper;
import com.syyo.gulimall.coupon.service.SeckillSessionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 秒杀活动场次
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 09:35:54
 */
@Service
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionMapper, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSessionMapper seckillSessionMapper;

    @Autowired
    private SeckillSkuRelationMapper seckillSkuRelationMapper;


    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(SeckillSessionReq req) {
        SeckillSessionEntity entity = new SeckillSessionEntity();
        // 添加创建时间和更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = seckillSessionMapper.insert(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10001.getCode(), ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = seckillSessionMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(SeckillSessionReq req) {
        SeckillSessionEntity entity = new SeckillSessionEntity();
        // 添加更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = seckillSessionMapper.updateById(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public SeckillSessionEntity findOne(Integer id) {
        return seckillSessionMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<SeckillSessionEntity> findAll(Integer pageNum, Integer pageSize, SeckillSessionReq req) {
        Page<SeckillSessionEntity> teacherPage = new Page<SeckillSessionEntity>(pageNum, pageSize);
        QueryWrapper<SeckillSessionEntity> wrapper = new QueryWrapper<>();
        IPage<SeckillSessionEntity> page = seckillSessionMapper.selectPage(teacherPage, wrapper);
        return page;
    }

    /**
     * 获取最近活动
     * @return
     */
    @Override
    public List<SeckillSessionEntity> lastSession() {

        //计算最近三天
        //查出这三天参与秒杀活动的商品
        List<SeckillSessionEntity> list = this.baseMapper.selectList(new QueryWrapper<SeckillSessionEntity>()
                .between("start_time", startTime(), endTime()));

        if (list != null && list.size() > 0) {
            List<SeckillSessionEntity> collect = list.stream().map(session -> {
                Long id = session.getId();
                //查出sms_seckill_sku_relation表中关联的skuId
                List<SeckillSkuRelationEntity> relationSkus = seckillSkuRelationMapper.selectList(new QueryWrapper<SeckillSkuRelationEntity>()
                        .eq("promotion_session_id", id));
                session.setRelationSkus(relationSkus);
                return session;
            }).collect(Collectors.toList());
        }
        return list;
    }

    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(plus, max);

        //格式化时间
        String endFormat = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return endFormat;
    }

    private String startTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now, min);

        //格式化时间
        String startFormat = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return startFormat;

    }


}